package com.lovo.csc.controller;


import com.lovo.csc.entity.Vendor;
import com.lovo.csc.entity.Vendorauditinfo;
import com.lovo.csc.service.IVendorService;
import com.lovo.csc.service.IVendorauditinfoService;
import com.lovo.psc.service.IPscUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/vendor")
@Api(tags = "供应商注册信息控制层--曾亚军--002")
public class VendorController {
    @Autowired
    IVendorService vendorService;
    @Autowired
    IVendorauditinfoService vendorInfoService;
    @Autowired
    IPscUserService  pscUserService;
    @GetMapping("findAll")
    @ApiOperation("查询全部供应商")
    public List<Vendor> findByItem(){
        return vendorService.list();
    }
    @PutMapping("updateState")
    @ApiOperation("修改供应商审核状态")
    public String updateState(String vendorId,String state){
        Vendor vendor = vendorService.getById(vendorId);
        if (!state.equals("1")){
            //修改本地
            vendor.setVendoraudit("2");

        }else{
            vendor.setVendoraudit(state);
        }
        vendorService.updateById(vendor);
        //修改供应商方
        String code = vendor.getVendorcode();
        pscUserService.upDataSupplierState(code,state);

        //审核记录
        Vendorauditinfo vendorauditinfo = new Vendorauditinfo();
        vendorauditinfo.setVendorcode(code);
        vendorauditinfo.setAuditpeople("zy");
        vendorauditinfo.setAuditstate(state);
        String time = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now());
        vendorauditinfo.setAuditdate(LocalDate.parse(time));
        vendorInfoService.save(vendorauditinfo);
        return "ok";
    }
}
